const constants = require('./constants.js');

const promise = require('./promise.js');

const QQMapWX = require('./qqmap-wx-jssdk.min.js');

const qqmapsdk = new QQMapWX({
    key: 'NVJBZ-4JE6X-UU44N-ZFUVT-NJH6Q-LCFZS'
});
const EXPIRE_TIME = 1000 * 60 * 5;

const getLocation = () => {
    return new promise((resolve, reject) => {
        var locationTmp = uni.getStorageSync(constants.getLocationStorageKey()); //看缓存中是否还存有位置信息，且是否过期

        if (locationTmp && locationTmp.expireTime >= new Date().getTime()) {
            resolve(locationTmp);
        } else {
            //获取位置信息
            uni.getLocation({
                type: 'gcj02',
                success: (res) => {
                    //成功则返回位置信息，并缓存
                    res.expireTime = new Date().getTime() + EXPIRE_TIME;
                    uni.setStorage({
                        key: constants.getLocationStorageKey(),
                        data: res
                    });
                    resolve(res);
                },
                fail: (err) => {
                    console.log(err); //失败了则去获取授权

                    uni.authorize({
                        scope: 'scope.userLocation',
                        success: (res) => {
                            //用户授权后再获取微信信息
                            uni.getLocation({
                                type: 'gcj02',
                                success: (res) => {
                                    res.expireTime = new Date().getTime() + EXPIRE_TIME;
                                    uni.setStorage({
                                        key: constants.getLocationStorageKey(),
                                        data: res
                                    });
                                    resolve(res);
                                },
                                fail: (err) => {
                                    console.log(err);
                                    reject(err);
                                }
                            });
                        },
                        fail: (err) => {
                            console.log(err); //用户拒绝授权，或之前已拒绝过，则检查用户的授权信息

                            uni.getSetting({
                                success: (res) => {
                                    //若用户拒绝了授权，则返回
                                    console.log(res);

                                    if (undefined != res.authSetting['scope.userLocation'] && !res.authSetting['scope.userLocation']) {
                                        reject('AUTH_REFUSED');
                                    } else {
                                        //若根本未请求过用户授权，或用户已授权了，则说明前面过程产生了不是因为授权问题而产生的错误，则直接返回
                                        reject('ERROR');
                                    }
                                },
                                fail: (err) => {
                                    console.log(err);
                                    reject(err);
                                }
                            });
                        }
                    });
                }
            });
        }
    });
};

const getLocationAddress = () => {
    return new promise((resolve, reject) => {
        var locationAddressTmp = uni.getStorageSync(constants.getLocationAddressStorageKey()); //看缓存中是否还存有位置信息，且是否过期

        if (locationAddressTmp && locationAddressTmp.expireTime >= new Date().getTime()) {
            resolve(locationAddressTmp);
        } else {
            var location = uni.getStorageSync(constants.getLocationStorageKey());

            if (!location) {
                reject('ERROR');
            } else {
                qqmapsdk.reverseGeocoder({
                    location: {
                        latitude: location.latitude,
                        longitude: location.longitude
                    },
                    success: (res) => {
                        let address = res.result.address_component;
                        address.expireTime = new Date().getTime() + EXPIRE_TIME;
                        uni.setStorage({
                            key: constants.getLocationAddressStorageKey(),
                            data: address
                        });
                        resolve(res.result.address_component);
                    },
                    fail: (err) => {
                        console.log(err);
                        reject(err);
                    }
                });
            }
        }
    });
};

module.exports = {
    getLocation: getLocation,
    getLocationAddress: getLocationAddress
};
